import 'dart:convert';

import 'package:fluintl/fluintl.dart';
import 'package:flutter/material.dart';
import 'package:x3_base_core/base_utils/base_utils.dart';
import 'package:x3_base_core/base_theme/base_themes.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:x3_base_core/base_models/base_token_model.dart';
import 'package:x3_base_core/base_enums/base_storage_enum.dart';
import 'package:x3_base_core/base_utils/base_dio_code_util.dart';
import 'package:x3_base_core/base_models/base_locale_model.dart';
import 'package:x3_base_core/base_models/base_permission_model.dart';

class BaseStorageUtil {
  BaseStorageUtil._();

  /// 静态私有变量
  static SharedPreferences _preferences;

  /// 提供初始化的方法
  static Future getInstance() async {
    _preferences = await SharedPreferences.getInstance();
  }

  /// 存储 - 异步
  static Future<bool> save(BaseStorageEnum key, value) {
    return _preferences.setString(key.toString(), value);
  }

  /// 取值
  static get(BaseStorageEnum key) {
    return _preferences.get(key.toString());
  }

  /// 移除
  static Future<bool> remove(BaseStorageEnum key) {
    return _preferences.remove(key.toString());
  }

  /// 清除Token 和 用户信息 和 权限
  static Future<bool> clearUserInfo() {
    remove(BaseStorageEnum.tokenInfo);
    remove(BaseStorageEnum.permissions);
    remove(BaseStorageEnum.cloudAuthorization);
    return remove(BaseStorageEnum.authorization);
  }

  /// 获取用户基本信息
  static BaseTokenModel getTokenInfo() {
    String storageUserData = get(BaseStorageEnum.tokenInfo);
    return BaseUtils.isNotEmpty(storageUserData) == true ? BaseTokenModel.fromJson(json.decode(storageUserData)) : BaseTokenModel();
  }

  /// 保存用户基本信息
  static Future<bool> saveTokenInfo(BaseTokenModel info) {
    return save(BaseStorageEnum.tokenInfo, info.toString());
  }

  /// 保存token
  static Future<bool> saveAuthorization(String authorization) {
    return save(BaseStorageEnum.authorization, authorization.toString());
  }

  /// 保存token 云授权 Token信息 "cloud-authorization"
  static Future<bool> saveCloudAuthorization(String cloudAuthorization) {
    return save(BaseStorageEnum.cloudAuthorization, cloudAuthorization.toString());
  }

  /// 保存登录接口响应内容
  static Future<bool> saveLoginResponse(String loginResponse) {
    return save(BaseStorageEnum.loginResponse, loginResponse.toString());
  }

  /// 保存 校验前 token
  static Future<bool> saveCheckBeforeAuthorization(String checkBeforeAuthorization) {
    return save(BaseStorageEnum.checkBeforeAuthorization, checkBeforeAuthorization.toString());
  }

  /// 保存 校验后 token
  static Future<bool> saveCheckAfterAuthorization(String checkAfterAuthorization) {
    return save(BaseStorageEnum.checkAfterAuthorization, checkAfterAuthorization.toString());
  }

  /// 获取token
  static String getAuthorization() {
    String authorization = get(BaseStorageEnum.authorization);
    return authorization;
  }

  /// 获取token 云授权 Token信息 "cloud-authorization"
  static String getCloudAuthorization() {
    String cloudAuthorization = get(BaseStorageEnum.cloudAuthorization);
    return cloudAuthorization;
  }

  /// 获取登录接口响应内容
  static String getLoginResponse() {
    String loginResponse = get(BaseStorageEnum.loginResponse);
    return loginResponse;
  }

  /// 获取 校验前 token
  static String getCheckBeforeAuthorization() {
    String checkBeforeAuthorization = get(BaseStorageEnum.checkBeforeAuthorization);
    return checkBeforeAuthorization;
  }

  /// 获取 校验后 token
  static String getCheckAfterAuthorization() {
    String checkAfterAuthorization = get(BaseStorageEnum.checkAfterAuthorization);
    return checkAfterAuthorization;
  }

  /// 保存当前的环境
  static Future<bool> saveCurrentEnvironment(String currentEnvironment) {
    return save(BaseStorageEnum.currentEnvironment, currentEnvironment.toString());
  }

  /// 获取当前的环境
  static String getCurrentEnvironment() {
    String currentEnvironment = get(BaseStorageEnum.currentEnvironment);
    return currentEnvironment;
  }

  /// 保存权限
  static Future<bool> savePermissions(List<BasePermissionModel> permissions) {
    String data = jsonEncode(permissions ?? []);
    return save(BaseStorageEnum.permissions, data);
  }

  /// 获取权限
  static List<BasePermissionModel> getPermissions() {
    String permissionsData = get(BaseStorageEnum.permissions);
    List<BasePermissionModel> permissions = [];
    var jsonList = jsonDecode(permissionsData) as List;
    jsonList.forEach((e) {
      permissions.add(BasePermissionModel.fromJson(e));
    });
    return permissions;
  }

  /// 获取当前手机的多语言
  static String getCurrentPhoneLanguage(BuildContext context) {
    String locale = get(BaseStorageEnum.locale);
    if (context != null) {
      String languageCode = CustomLocalizations.of(context).locale.languageCode;
      String countryCode = CustomLocalizations.of(context).locale.countryCode;
      return '$languageCode-$countryCode'.toLowerCase();
    }
    if (locale != null) {
      Map map = BaseDioCodeUtil.decodeMapResult(locale);
      BaseLocaleModel model = BaseLocaleModel.fromJson(map);
      String code = model.regionCode + '-' + model.countryCode;
      return code.toLowerCase();
    }
    return 'zh-cn';
  }

  /// 获取存储的主题颜色
  static Color get storagePrimaryColor {
    String key = BaseStorageUtil.get(BaseStorageEnum.theme);
    if (BaseThemes.colors.containsKey(key)) {
      return BaseThemes.colors[key];
    }
    return BaseThemes.defaultPrimaryColor;
  }

  /// 获取存储的多语言
  static BaseLocaleModel get storageLocale {
    String locale = BaseStorageUtil.get(BaseStorageEnum.locale);
    if (locale == null) {
      return null;
    }
    Map map = BaseDioCodeUtil.decodeMapResult(locale);
    return BaseLocaleModel.fromJson(map);
  }

  /// 获取存储的是否开启网络调试模式
  static bool get storageShowLogs {
    String showLog = BaseStorageUtil.get(BaseStorageEnum.showLogs);
    return showLog == 'true';
  }

  /// 获取Ftp地址
  static String get storageFtpService {
    String ftpService = BaseStorageUtil.get(BaseStorageEnum.FtpService);
    return ftpService ?? '';
  }
}
